package com.example.myapplication.ui.components


import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.example.myapplication.ViewModel.MainViewModel
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.launch
import java.sql.Time
import java.util.*

@OptIn(ExperimentalPagerApi::class)
@Composable
fun SwiperContent(vm: MainViewModel = viewModel()) {

    // 虚拟页数
    // Int.MAX_VALUE int型的最大值
    // 直接设置virtualPage,会导致真实数据list越界(找不到这么大的值),应用会闪退
    val virtualPage = Int.MAX_VALUE

    // 实际页数
    val actualPage = vm.swiperList.size

    // 初始图片下标
    // 取总数中间的值
    val initialIndex = virtualPage / 2

    // 轮播图初始位置
    val pagerState = rememberPagerState(initialPage = initialIndex)

    val coroutineScope = rememberCoroutineScope()

    // 自动轮播定时器(需要销毁,故使用DisposableEffect)
    DisposableEffect( Unit ) {
        var timer = Timer()

        timer.schedule(object : TimerTask() {
            override fun run() {
                coroutineScope.launch {
                    // scrollToPage 滚动到某一页
                    // scrollToPage挂起函数需要协程
                    pagerState.scrollToPage(pagerState.currentPage + 1)
                }
            }
        }, 3000, 3000)

        // 销毁定时器
        onDispose {
            timer.cancel()
        }
    }

    // aspectRatio 图片比例
    // contentScale 内容裁剪
    // itemSpacing 内容之间的间距
    // HorizontalPager 横向滚动
    HorizontalPager(
        state = pagerState,
        count = virtualPage,
        itemSpacing = 16.dp,
        modifier = Modifier
            .padding(horizontal = 8.dp)
            .clip(RoundedCornerShape(8.dp))
    ) { index ->

        // index.floorDiv(actualPage) index / actualPage 并对结果进行向下取整
        // val currentIndex = this - (this.floorDiv(other)) * other
        // index - initialIndex实际下标减去初始下标,可以使初始图片固定为第一张,而不是中间的那张
        val currentIndex = (index - initialIndex).floorMod(actualPage)

        AsyncImage(
            model = vm.swiperList[currentIndex].imageUrl,
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .aspectRatio(7 / 3f),
            contentScale = ContentScale.Crop
        )
    }
}

fun Int.floorMod(other: Int): Int = when(other) {
    0 -> this
    else -> this - (this.floorDiv(other)) * other
}
